package com.module;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import DateBase.Datebase;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.valuebean.ADOrder;
import com.valuebean.ADUser;

public class ADUserModule {
	Datebase con=new Datebase();
	Connection conn=(Connection) con.getConnection();
	public ArrayList selectMsg() throws SQLException{
		String sql="select * from user";
		Statement st=(Statement) conn.createStatement();
		ResultSet rs=st.executeQuery(sql);
		ArrayList list = new ArrayList();
		while(rs.next()){
			ADUser u=new ADUser();
			u.setUseraddress(rs.getString("useraddress"));
			u.setUserid(rs.getInt("userid"));
			u.setUserimg(rs.getString("userimg"));
			u.setUsername(rs.getString("username"));
			u.setUserpassword(rs.getString("userpassword"));
			list.add(u);
		}
		return list;
	}
	public int delAction(int userid) throws SQLException{
		String sql="delete from user where userid="+userid+"";
		Statement st=(Statement) conn.createStatement();
		int flag=st.executeUpdate(sql);
		if(flag==1)
			return 1;
		else
			return 0;
	}
	
	public int updateAction(String username,String userpassword,int userid) throws SQLException{
		String sql="update user set username ='"+username+"',userpassword='"+userpassword+"' where userid = "+userid+"";
		Statement st=(Statement) conn.createStatement();
		int flag=st.executeUpdate(sql);
		if(flag==1)
			return 1;
		else
			return 0;
	}
	public int insertAction(String username,String userpassword) throws SQLException{
		String sql="insert into  user (username,userpassword) values ('"+username+"','"+userpassword+"')";
		Statement st=(Statement) conn.createStatement();
		int flag=st.executeUpdate(sql);
		System.out.print(sql);
		if(flag==1)
			return 1;
		else
			return 0;
	}
}
